package crawler;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import util.$;
import util.Properties;
import edu.uci.ics.crawler4j.crawler.CrawlController;
import edu.uci.ics.crawler4j.frontier.Frontier;

public class Crawlers {
	
	private Map<String,CrawlController> crawlers;
	
	public Crawlers(){
		crawlers = new HashMap<String,CrawlController>();
	}
	
	public void addCrawler(String key, CrawlController crawler){
		
		crawlers.put(key, crawler);
		
	}
	
	public CrawlController getCrawler(String key){

		return crawlers.get(key);
		
	}
	
	public boolean hasCrawler(String key){
		return crawlers.containsKey(key);
	}
	
	public void shutdownAll(){
		for(String key : crawlers.keySet()){
			CrawlController controller = crawlers.get(key);
			//Frontier frontier = controller.getFrontier();
			//if(frontier != null){
			//	frontier.close();
			//}
			controller.shutdown();
			controller.waitUntilFinish();
		}
	}
	
	public String toJSON(){
		List<Map<String,String>> list = new ArrayList<Map<String,String>>();
		
		for(String key : crawlers.keySet()){
			Map<String,String> map = new HashMap<String,String>();
			map.put("storeId", key);
			CrawlController controller = crawlers.get(key);
			Properties properties = (Properties) controller.getCustomData();
			map.put("crawlId", properties.getString("crawlId"));
			map.put("seed", properties.getString("seed"));
			map.put("finished", String.valueOf(controller.isFinished()));
			map.put("NumberOfProcessedPages",String.valueOf(controller.getFrontier().getNumberOfProcessedPages()));
			map.put("QueueLength",String.valueOf(controller.getFrontier().getQueueLength()));
			map.put("DocCount",String.valueOf(controller.getDocIdServer().getDocCount()));
			list.add(map);
		}
		return $.toJSON("crawlers",list);
	}
}
